package com.admin.system.controller;

import com.admin.model.base.PageResult;
import com.admin.model.pojo.Job;
import com.admin.model.dto.JobDto;
import com.admin.system.service.JobService;
import com.admin.system.service.mapstruct.JobMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

/**
 * (Job)表控制层
 *
 * @author makejava
 * @since 2021-01-31 20:22:08
 */
@Api(tags = "岗位")
@RestController
@RequestMapping("job")
public class JobController {
    /**
     * 服务对象
     */
    @Resource
    private JobService jobService;


    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "createdBy", "updatedBy", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('job:add')")
    @PostMapping
    public ResponseEntity<Object> create(@Validated({JobDto.Create.class, Default.class}) @RequestBody JobDto jobDto) {
        Job job = jobService.create(jobDto);
        return ResponseEntity.ok(job);
    }

    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('job:list')")
    @GetMapping("/{id}")
    public ResponseEntity<Object> findById(@PathVariable Long id) {
        return ResponseEntity.ok(jobService.findById(id));
    }


    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('job:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteByIds(@RequestBody Set<Long> ids) {
        jobService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("岗位下拉列表")
    @GetMapping("/list")
    public ResponseEntity<Object> findList(){
        List<LinkedHashMap<String, Object>> list = jobService.findList();
        return ResponseEntity.ok(list);
    }


    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('job:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(defaultValue = "-create_time") String sort,
                                          @RequestBody(required = false) JobDto jobDto) {
        PageResult pageResult = jobService.queryAll(jobDto, pageNum, pageSize, sort);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('job:edit')")
    @PutMapping
    public ResponseEntity<Object> updateById(@Validated({JobDto.Update.class, Default.class}) @RequestBody JobDto jobDto) {
        Job job = jobService.updateById(jobDto);
        return ResponseEntity.ok(job);
    }


}